﻿using Microsoft.AspNetCore.Mvc;
using MyFile.Config;
using MyFile.Service;
using MyFile.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFile.Controllers
{
    [ApiController]
    public class H5DBController
    {
        private DBHelper db = DBHelper.Instance;

        ///-------------------------------------------------------------------
        /// public H5DBResult GetUser([FromQuery] UserConfig userConfig)
        /// 
        /// 以上为Get请求，参数为UserConfig类型，返回值为H5DBResult类型
        /// 
        /// public DefaultResult GetUser([FromBody] UserConfig userConfig)
        /// 
        /// 以上为Post请求，参数为UserConfig类型，返回值为DefaultResult类型
        ///-------------------------------------------------------------------

        /// <summary>
        /// 悠悠添加饰品
        /// </summary>
        /// <param name="addGoodConfig"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/AddPost")]
        public DefaultResult AddPost([FromBody] UUAddGoodConfig addGoodConfig)
        {
            try
            {
                string result = "";
                if (addGoodConfig.keyWord == "No")
                {
                    result = UU.Instance.PostUU(addGoodConfig.templateid);
                }
                else
                {
                    result = UU.Instance.PostUU(addGoodConfig.templateid, addGoodConfig.keyWord);
                }
                if (result == "OK")
                    return new DefaultResult() { Code = 200, Msg = "添加成功" };
                else
                    return new DefaultResult() { Code = 500, Msg = $"失败，{result}" };
            }
            catch (Exception ex)
            {
                return new DefaultResult() { Code = 500, Msg = ex.Message };
            }
        }
        
        /// <summary>
        /// 悠悠删除饰品
        /// </summary>
        /// <param name="deleteGoodConfig"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/DeletePost")]
        public DefaultResult DeletePost([FromBody] UUDeleteGoodConfig deleteGoodConfig)
        {
            try
            {
                DBGoodsList dB = DBHelper.Instance.mDB.CopyNew().Queryable<DBGoodsList>().Where(a => a.templateId.ToString() == deleteGoodConfig.templateid && a.doppler ==deleteGoodConfig.doppler ).First();
                bool result = UU.Instance.DeleteUU(deleteGoodConfig);
                foreach(var item in UU.Instance.good)
                {
                    if(item.GoodsName == dB.stickerName)
                    {
                        UU.Instance.good.Remove(item);
                        string json = JsonConvert.SerializeObject(UU.Instance.good);
                        File.WriteAllText(Path.Combine(Program.path, "Config.json"), json);
                        break;
                    }
                }
                if (result)
                    return new DefaultResult() { Code = 200, Msg = "删除成功" };
                else
                    return new DefaultResult() { Code = 500, Msg = "失败" };
            }
            catch (Exception ex)
            {
                return new DefaultResult() { Code = 500, Msg = ex.Message };
            }
        }

        [HttpGet]
        [Route("api/GetGoodsList")]
        public List<GoodsTime> GetGoodsList()
        {
            string configfilepath = Path.Combine(Program.path, "Config.json");
            string config = File.ReadAllText(configfilepath);
            return JsonConvert.DeserializeObject<List<GoodsTime>>(config);
        }
    }
    public class UUAddGoodConfig
    {
        public string templateid { get; set; }
        public string keyWord { get; set; }
    }
    public class UUDeleteGoodConfig
    {
        public string templateid { get; set; }
        public string doppler { get; set; }
    }
}
